﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using System.Data.SqlClient;

namespace MovieRental.SqlRepositoryLibrary.Helpers
{
    public class SqlEntityMapping : IEnumerable<ColumnMetadata>
    {
        private Dictionary<string, ColumnMetadata> _mappings = new Dictionary<string,ColumnMetadata>();

        public SqlEntityMapping(string tableName, string idPropertyName)
        {
            TableName = tableName;
            IdPropertyName = idPropertyName;
        }

        public string TableName { set; get; }

        public string IdPropertyName { set; get; }

        public string IdColumnName
        {
            get { return this[IdPropertyName].Name; }
        }

        public void AddProperty(string propertyName, string columnName, SqlDbType type)
        {
            _mappings.Add(propertyName, new ColumnMetadata(columnName, type));
        }

        public bool RemoveProperty(string propertyName)
        {
            return _mappings.Remove(propertyName);
        }

        public ColumnMetadata this[string propertyName]
        {
            get { return _mappings[propertyName]; }
        }

        public IEnumerable<string> Properties
        {
            get { return _mappings.Keys; }
        }

        public IEnumerable<string> Columns
        {
            get { return _mappings.Values.Select(x => x.Name); }
        }

        public IEnumerator<ColumnMetadata> GetEnumerator()
        {
            return _mappings.Values.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _mappings.Values.GetEnumerator();
        }
    }
}
